package com.dmyang.service;

import com.dmyang.dao.DoctorMapper;
import com.dmyang.dao.PatientMapper;
import com.dmyang.entity.Doctor;
import com.dmyang.entity.Patient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * Created by brss on 2017/7/20.
 */
@Service("userServiceImpl")
public class UserServiceImpl implements UserService {

    @Autowired
    private PatientMapper patientMapper;

    @Autowired
    private DoctorMapper doctorMapper;

    /**
     * 创建病人
     *
     * @param patient
     * @return
     */
    @Override
    @Transactional
    public int createPatient(Patient patient) {
        patientMapper.insertSelective(patient);
        return 0;
    }

    /**
     * 由用户名，密码查询用户
     *
     * @param patient
     * @return
     */
    @Override
    @Transactional
    public Patient queryPatByUsernameAndPwd(Patient patient) {
        Patient p = patientMapper.selectByUsernameAndPwd(patient);
        return p;
    }

    /**
     * 判断医生是否第一次登录（用主键查不到记录是返回false）
     * @param doctor
     * @return
     */
    @Override
    public boolean isDocFirstLogin(Doctor doctor) {

        Doctor doctor1 = doctorMapper.selectByPrimaryKey(doctor.getPk());
        if (doctor1 == null) {
            return false;
        }
        if (doctor1.getPassword()==null)
            return true;
        else
            return false;
    }

    /**
     * 由医生通行码和密码查询医生
     * @param doctor
     * @return
     */
    @Override
    public Doctor queryDocByPkAndPwd(Doctor doctor) {

        Doctor doctor1 = doctorMapper.selectByPkAndPwd(doctor);

        return doctor1;
    }
}
